import numpy as np

def weighted_l2_loss(x, target):
    distL1 = x - target
    distL2 = distL1 ** 2

    num = x.shape[0] * x.shape[1]
    
    w = 2 * num / (np.sum(x) + np.sum(target))

    mask = np.ones(x.shape)
    mask[target > 0] = w
    mask[x > 0] = w

    return np.sum(distL2 * mask) / num